/**
 * This file is part of Megasquirt Logger for PalmOS.
 * Copyright (C) 2011 Andrew Beard
 *
 * Megasquirt Logger is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Megasquirt Logger is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Megasquirt Logger.  If not, see <http://www.gnu.org/licenses/>.
 */
 
#pragma once
#include <SerialMgr.h>

class MegasquirtInterface
{
  public:
    MegasquirtInterface( UInt16 nPortID,
                         UInt16 nRTU_Length );
	~MegasquirtInterface();

  public:
    void         ProcessRxData              ( void );
    void         RequestSignature           ( void );
    void         RequestRealtimeUpdate      ( void );
    bool         ReceivedSignature          ( void ) const;
    const Char*  GetSignature               ( void ) const;
    const UInt8* GetRealtimeUpdateData      ( void ) const;
    bool         CheckSignatureEvent        ( void );
    bool         CheckRealtimeUpdateEvent   ( void );

  private:
    static void  RequestedDataReadyCallback ( UInt32 refCon );

  private:
	static const UInt16 SIGNATURE_LENGTH        = 20;
	static const UInt16 RX_BUFFER_SIZE          = 256;
	
	static const UInt8  REALTIME_UPDATE_COMMAND = 'A';
	static const UInt8  SIGNATURE_COMMAND       = 'Q';

	typedef enum
	{
		CONNECTED,
		WAITING_FOR_SIGNATURE,
		WAITING_FOR_REALTIME_UPDATE
	} E_MegasquirtInterfaceStateType;

  private:
    Err                            m_LastError; 
	E_MegasquirtInterfaceStateType m_eInterfaceState;
	UInt16                         m_nSerialPortID;
	UInt16                         m_nRealtimeUpdateLength;
    bool                           m_bSignatureReceived;
	Char                           m_Signature[ SIGNATURE_LENGTH + 1 ];
	UInt8                          m_RxBuffer[ RX_BUFFER_SIZE ];
	bool                           m_bRealtimeUpdateEvent;
	bool                           m_bSignatureEvent;
	UInt8*                         m_pRealtimeUpdateData;
	bool                           m_bDataReady;
};